home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Files / GetOwnerGroup / GetOwnerGroup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-07  |  7.2 KB  |  311 lines  |  [TEXT/KAHL]

  1. #include "simple.h"
  2.  
  3. /*****
  4.  * a structure used to pre-select a folder
  5.  *****/
  6. typedef struct {
  7.     FSSpec                *theFile;
  8.     StandardFileReply    *theReply;
  9. } MyDataRecord;
  10.  
  11.  
  12. #define isDirectory     0x0010
  13. #define kReportDlgID    257
  14. #define kSFPutDlgID        256
  15.  
  16.  
  17. /** PROTOTYPES for MoreFiles functions **/
  18.  
  19. pascal    OSErr    HGetDirAccess(short vRefNum,
  20.                               long dirID,
  21.                               Str255 name,
  22.                               long *ownerID,
  23.                               long *groupID,
  24.                               long *accessRights);
  25.  
  26. pascal    OSErr    FSpGetDirAccess(const FSSpec *spec,
  27.                                 long *ownerID,
  28.                                 long *groupID,
  29.                                 long *accessRights);
  30.                                 
  31. pascal    OSErr    HMapID(Str255 volName,
  32.                        short vRefNum,
  33.                        long ID,
  34.                        short objType,
  35.                        Str255 name);
  36.  
  37.  
  38.  
  39. /*****
  40.  * On first call through CustomGetFile(), set everything so folder specified in 
  41.  * myDataPtr->theFile is preselected
  42.  *****/
  43. pascal short dlgHook(short item, DialogPtr theDialog, MyDataRecord *myDataPtr)
  44. {
  45.     if (item == sfHookFirstCall)
  46.     {
  47.         myDataPtr->theReply->sfFile = *(myDataPtr->theFile);
  48.         
  49.         // the following line ensures the first subfolder in the folder specified
  50.         // is selected.  If we didn't nil out the name, the last subfolder would
  51.         // be selected.
  52.         myDataPtr->theReply->sfFile.name[0] = 0;
  53.         myDataPtr->theReply->sfScript = 0;
  54.         return sfHookChangeSelection;
  55.     }
  56.     return item;
  57. }
  58.  
  59.  
  60. /*****
  61.  * Display a folder at the location specified by myFSSpec.  The files within this
  62.  * folder are grayed out.  We return with a full FSSpec, or -1 if user hit cancel.
  63.  *****/
  64. OSErr GetFileGray(FSSpec *myFSSpec)
  65. {
  66.     OSErr            err;
  67.     
  68.     
  69.     MyDataRecord    myData;
  70.     
  71.     /*
  72.      * the default name is used in building a FSSpec for the reply.  It must
  73.      * not be passed in as a null string, or your "select this folder" button
  74.      * won't ever be active.
  75.      */
  76.     Str255                defaultName = "\pMust Not Be Null";
  77.     StandardFileReply    reply;
  78.     Point                where = {-1, -1};        // autocenter, System 7!
  79.     short                activeList[2] = {1, 7};    // only keyboard on file list
  80.         
  81.     myData.theFile = myFSSpec;
  82.     myData.theReply = &reply;
  83.     
  84.     CustomPutFile("\p", 
  85.                   defaultName, 
  86.                   &reply, 
  87.                   kSFPutDlgID, 
  88.                   where, 
  89.                   (DlgHookYDProcPtr)dlgHook, 
  90.                   nil, 
  91.                   activeList, 
  92.                   nil, 
  93.                   &myData);
  94.     if (reply.sfGood)
  95.         err = FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, nil, myFSSpec);
  96.     else
  97.         err = -1;        // return some arbitrary error telling us cancel was selected.
  98.     return err;
  99. }
  100.  
  101.  
  102. /***** 
  103.  * Set up a file spec to point to System file in System Folder 
  104.  *****/
  105. OSErr SetUpFile(FSSpec *fileSpec)
  106. {
  107.     OSErr    err;
  108.     short    vRefNum;
  109.     long    dirID;
  110.     
  111.     err = FindFolder(kOnSystemDisk,kSystemFolderType,kDontCreateFolder, &vRefNum,&dirID);
  112.     
  113.     if (err) {
  114.         DebugStr("\pFickle FindFolder failed to find fey folder");
  115.         ExitToShell();
  116.     }
  117.     
  118.     err = FSMakeFSSpec(vRefNum,dirID,"\pSystem",fileSpec);
  119.     
  120.     if (err) {
  121.         DebugStr("\pFaulty FSMakeFSSpec fails in forming FSSpec.");
  122.         ExitToShell();
  123.     }
  124.     return err;
  125.   
  126. }
  127.  
  128. /******
  129.  * Report on file, group, and owner selected
  130.  * Really hacky, but it seems to do the job -- VDM 9/23/94
  131.  ******/
  132. void Report(FSSpec *theFile)
  133. {
  134.     long    theOwnerID,                /* owner of the parent directory */
  135.             theGroupID,                /* group with access to parent directory */
  136.             theAccessRights;        /* access rights to parent directory */
  137.     OSErr    myErr;
  138.     Str255    theOwnerName;
  139.     Str255    theGroupName;
  140.     
  141.     myErr = FSpGetDirAccess ( theFile, &theOwnerID, &theGroupID, &theAccessRights );
  142.     
  143.     if (myErr == noErr)
  144.     {
  145.         myErr = HMapID ( nil, theFile->vRefNum, theOwnerID, 1, theOwnerName );        /* passing 1 gives us owner info */
  146.         if (myErr == noErr)
  147.             myErr = HMapID (nil, theFile->vRefNum, theGroupID, 2, theGroupName );    /* passing 2 gives us group info */
  148.         else
  149.             DebugStr("\pTrouble getting owner name!");
  150.     }
  151.     else
  152.     {
  153.         DebugStr("\pWe're hosed with GetDirAccess!");
  154.     }
  155.     
  156.     if (myErr == noErr)
  157.     {
  158.         ParamText(theFile->name, theOwnerName, theGroupName, "\p");
  159.         Alert (kReportDlgID, nil);    
  160.     }
  161.     
  162.     else
  163.         DebugStr("\pCannot get the group name!");
  164.     
  165. }
  166.  
  167. /******
  168.  * Test Displaying folders only, with files within the folders in gray
  169.  ******/
  170. void TestGetFileGray(void)
  171. {
  172.     OSErr    err;
  173.     FSSpec    theFile;
  174.     
  175.     err = SetUpFile(&theFile);
  176.     if (!err)
  177.     {
  178.         err = GetFileGray(&theFile);
  179.         if (!err)
  180.             Report(&theFile);
  181.     }
  182. }
  183.  
  184. /******
  185.  * if the isDirectory bit is set, return false (to show the directory)
  186.  * if the isDirectory bit is clear, return true (to filter out all files)
  187.  ******/
  188. pascal Boolean MyStandardFileFilter(CInfoPBPtr pb, MyDataRecord *myDataPtr)
  189. {
  190.     return ( !(pb->dirInfo.ioFlAttrib & isDirectory) );
  191. }
  192.  
  193. /*****
  194.  * Handle items hit in our dialog
  195.  *****/
  196. pascal short MyDlgFilter(short item, DialogPtr theDialog, MyDataRecord *myDataPtr)
  197. {
  198.     if (item == sfHookFirstCall)
  199.     {
  200.         myDataPtr->theReply->sfFile = *(myDataPtr->theFile);
  201.         
  202.         // the following line ensures the first subfolder in the folder specified
  203.         // is selected.  If we didn't nil out the name, the last subfolder would
  204.         // be selected.
  205.         myDataPtr->theReply->sfFile.name[0] = 0;
  206.         myDataPtr->theReply->sfScript = 0;
  207.         return sfHookChangeSelection;
  208.     }
  209.     if (item == 10)
  210.         return sfItemOpenButton;
  211.     return item;
  212. }
  213.  
  214. /*****
  215.  * Test Displaying folder only, with no files within the folders.
  216.  *****/
  217. TestGetFileNoGray()
  218. {
  219.     FSSpec    myFSSpec;
  220.     SFTypeList    typeList;
  221.     StandardFileReply reply;
  222.     Point    where = {-1, -1};
  223.     MyDataRecord myData;
  224.     
  225.     //my variables VDM
  226.     OSErr    myErr;
  227.     long    ownerID;
  228.     long     groupID;
  229.     long    accessRights;
  230.     
  231.     
  232.  
  233.     myData.theFile = &myFSSpec;
  234.     myData.theReply = &reply;
  235.     
  236.     SetUpFile(&myFSSpec);
  237.     CustomGetFile((FileFilterYDProcPtr)MyStandardFileFilter,
  238.                   -1,
  239.                   typeList,
  240.                   &reply,
  241.                   -6042,
  242.                   where,
  243.                   (DlgHookYDProcPtr)MyDlgFilter,
  244.                   nil,
  245.                   0,
  246.                   nil,
  247.                   &myData);
  248.     if (reply.sfGood)
  249.     {
  250.         FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, nil, &myFSSpec);
  251.         myErr = HGetDirAccess(reply.sfFile.vRefNum, reply.sfFile.parID,  nil, &ownerID, &groupID, &accessRights);
  252.         Report(&myFSSpec);
  253.     }
  254. }
  255.  
  256. /****
  257. *Routines lifted from MoreFiles with a few modifications -- VDM - 9/23
  258. ****/
  259. /*****************************************************************************/
  260.  
  261. pascal    OSErr    HGetDirAccess(short vRefNum,
  262.                               long dirID,
  263.                               Str255 name,
  264.                               long *ownerID,
  265.                               long *groupID,
  266.                               long *accessRights)
  267. {
  268.     HParamBlockRec pb;
  269.     OSErr error;
  270.  
  271.     pb.accessParam.ioNamePtr = name;
  272.     pb.accessParam.ioVRefNum = vRefNum;
  273.     pb.fileParam.ioDirID = dirID;
  274.     error = PBHGetDirAccessSync(&pb);
  275.     *ownerID = pb.accessParam.ioACOwnerID;
  276.     *groupID = pb.accessParam.ioACGroupID;
  277.     *accessRights = pb.accessParam.ioACAccess;
  278.     return (error);
  279. }
  280.  
  281. /*****************************************************************************/
  282.  
  283. pascal    OSErr    FSpGetDirAccess(const FSSpec *spec,
  284.                                 long *ownerID,
  285.                                 long *groupID,
  286.                                 long *accessRights)
  287. {
  288.     return (HGetDirAccess(spec->vRefNum, spec->parID, (StringPtr)spec->name,
  289.                           ownerID, groupID, accessRights));
  290. }
  291.  
  292. /*****************************************************************************/
  293.  
  294. pascal    OSErr    HMapID(Str255 volName,
  295.                        short vRefNum,
  296.                        long ID,
  297.                        short objType,
  298.                        Str255 name)
  299. {
  300.     HParamBlockRec pb;
  301.  
  302.     pb.objParam.ioNamePtr = volName;
  303.     pb.objParam.ioVRefNum = vRefNum;
  304.     pb.objParam.ioObjType = objType;
  305.     pb.objParam.ioObjNamePtr = name;
  306.     pb.objParam.ioObjID = ID;
  307.     return (PBHMapIDSync(&pb));
  308. }
  309.  
  310. /*****************************************************************************/
  311.